package com.alibaba.basic.uitl;


import org.springframework.context.ApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件工具类
 */
public class EmailUtil{

    public static void send(String text, String people){
        try {
            // 通过上下文获得对象
            ApplicationContext context = SpringUtil.getContext();
            JavaMailSender javaMailSender = context.getBean(JavaMailSender.class);
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");

            // 从这里开始抽取，填写发送有关内容
            // 发送人
            helper.setFrom("353974909@qq.com");
            //
            helper.setSubject("宠物乐园审核结果通知");
            helper.setText(text);
            helper.setTo(people);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
